package com.cjq.system.service.impl;

import com.cjq.common.core.constant.UserConstants;
import com.cjq.common.core.exception.ServiceException;
import com.cjq.common.core.utils.StringUtils;
import com.cjq.system.domain.SysGoods;
import com.cjq.system.mapper.SysGoodsMapper;
import com.cjq.system.mapper.SysUserGoodsMapper;
import com.cjq.system.service.ISysGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 岗位信息 服务层处理
 *
 * @author ruoyi
 */
@Service
public class SysGoodsServiceImpl implements ISysGoodsService
{
    @Autowired
    private SysGoodsMapper goodsMapper;

    @Autowired
    private SysUserGoodsMapper userGoodsMapper;

    /**
     * 查询岗位信息集合
     *
     * @param goods 岗位信息
     * @return 岗位信息集合
     */
    @Override
    public List<SysGoods> selectGoodsList(SysGoods goods)
    {
        return goodsMapper.selectGoodsList(goods);
    }

    /**
     * 查询所有岗位
     *
     * @return 岗位列表
     */
    @Override
    public List<SysGoods> selectGoodsAll()
    {
        return goodsMapper.selectGoodsAll();
    }

    /**
     * 通过岗位ID查询岗位信息
     *
     * @param goodsId 岗位ID
     * @return 角色对象信息
     */
    @Override
    public SysGoods selectGoodsById(Long goodsId)
    {
        return goodsMapper.selectGoodsById(goodsId);
    }

    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    @Override
    public List<Long> selectGoodsListByUserId(Long userId)
    {
        return goodsMapper.selectGoodsListByUserId(userId);
    }

    /**
     * 校验岗位名称是否唯一
     *
     * @param goods 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkGoodsNameUnique(SysGoods goods)
    {
        Long goodsId = StringUtils.isNull(goods.getGoodsId()) ? -1L : goods.getGoodsId();
        SysGoods info = goodsMapper.checkGoodsNameUnique(goods.getGoodsName());
        if (StringUtils.isNotNull(info) && info.getGoodsId().longValue() != goodsId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验岗位编码是否唯一
     *
     * @param goods 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkGoodsCodeUnique(SysGoods goods)
    {
        Long goodsId = StringUtils.isNull(goods.getGoodsId()) ? -1L : goods.getGoodsId();
        SysGoods info = goodsMapper.checkGoodsCodeUnique(goods.getGoodsCode());
        if (StringUtils.isNotNull(info) && info.getGoodsId().longValue() != goodsId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param goodsId 岗位ID
     * @return 结果
     */
    @Override
    public int countUserGoodsById(Long goodsId)
    {
        return userGoodsMapper.countUserGoodsById(goodsId);
    }

    /**
     * 删除岗位信息
     *
     * @param goodsId 岗位ID
     * @return 结果
     */
    @Override
    public int deleteGoodsById(Long goodsId)
    {
        return goodsMapper.deleteGoodsById(goodsId);
    }

    /**
     * 批量删除岗位信息
     *
     * @param goodsIds 需要删除的岗位ID
     * @return 结果
     */
    @Override
    public int deleteGoodsByIds(Long[] goodsIds)
    {
        for (Long goodsId : goodsIds)
        {
            SysGoods goods = selectGoodsById(goodsId);
            if (countUserGoodsById(goodsId) > 0)
            {
                throw new ServiceException(String.format("%1$s已分配,不能删除", goods.getGoodsName()));
            }
        }
        return goodsMapper.deleteGoodsByIds(goodsIds);
    }

    /**
     * 新增保存岗位信息
     *
     * @param goods 岗位信息
     * @return 结果
     */
    @Override
    public int insertGoods(SysGoods goods)
    {
        return goodsMapper.insertGoods(goods);
    }

    /**
     * 修改保存岗位信息
     *
     * @param goods 岗位信息
     * @return 结果
     */
    @Override
    public int updateGoods(SysGoods goods)
    {
        return goodsMapper.updateGoods(goods);
    }
}
